<template>
  <div>
    <div class="node" v-has="{action: 'combo:count:statistics', effect: 'move'}">
    <div :class="[$style.search_box, $style.topSelect]">
      <div :class="$style.selectBox" style="margin-right:20px;display: flex;align-items: center;">
        <label>创建时间：</label>
        <el-date-picker
          v-model="searchs.appointmentTime"
          type="datetimerange"
          @change="getQueryData"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
         
          value-format="timestamp"
          :picker-options="pickerOptions"
          style="height:40px"
        ></el-date-picker>
      </div>
      <div :class="$style.selectBox">
        <label>时间分组类型：</label>
        <el-select
          style="width:160px"
          v-model="searchs.type"
          @change="getQueryData"
          placeholder="请选择类型"
        >
          <el-option label="日" value="date"></el-option>
          <el-option label="月" value="month"></el-option>
          <el-option label="年" value="year"></el-option>
        </el-select>
      </div>
       <div :class="$style.searchBox1" style="margin-left: 20px">
        <el-button type="primary" @click="getQueryData">查询</el-button>        
      </div>
    </div>
    <Panel title="套餐统计">
      <!-- <div slot='ext' :class="$style.examine">
        <div class="adopt" @click="()=>editStore()">新增</div>
    </div> -->
    <TableEncapsulation
      slot="body"
      :tableConfig="tableConfig"
      :tableData="tableData"
    >
   
    <template slot="operation" slot-scope="operation">
          <el-button
            type="text"
            @click="()=>editStore(operation.row.id)"
          >编辑</el-button>
        </template>
        <TablePager
          slot="pager"
          v-if="pager.use"
          :size="pager.size"
          :pageChange="pageChange"
          :show-total="pager.showTotal"
          :total="pager.total"
          :current="pager.current"
        >
          <span class="demonstration">GO></span>
        </TablePager>
    </TableEncapsulation>
    </Panel>
    </div>
    <div style="display: none;width:100%;height:100%;margin: auto;" v-has="{action: 'combo:count:statistics', effect: 'noPremiss'}">
      <NoPremission></NoPremission>
    </div>
  </div>
</template>
<script>
import request from "@/tools/request";
import Panel from "@/components/Panel";
import TableEncapsulation from "@/components/TableEncapsulation";
import TablePager from "@/components/Pager";
import SearchInput from "@/components/SearchInput";
import { objectFilter } from "@/tools/array";
import { dateFormat } from "@/tools/time";
export default {
  name: "comboAmout",
  components: {
    TableEncapsulation,
    TablePager,
    Panel,
    SearchInput
  },
  data() {
    return {
      tableData: [],
      searchs: {
        type: 'date',
        appointmentTime: [
            
        ],
      },
      pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
       pager: {
        use: true,
        total: 0,
        current: 1,
        size: 10,
        showTotal: true
      },
      operation: true,
    };
  },
  created() {
     let now = new Date().getTime() + 3600 * 1000 * 24
    let year = new Date(now).getFullYear()
    let month = new Date(now).getMonth() 
    let day = new Date(now).getDate()
    let last = new Date().getTime() - 3600 * 1000 * 24 * 6
    let lastYear =  new Date(last).getFullYear() 
    let LastMonth = new Date(last).getMonth() 
    let lastDay = new Date(last).getDate()
    this.searchs.appointmentTime = [ new Date(lastYear, LastMonth, lastDay, 0, 0,0), new Date(year, month, day, 0, 0,0)]
    this.getQueryData();
  },
  computed: {
    tableConfig () {
      return {
        selection: true,
        columnConfig: [
          {
            label: "日期",
            type: "text",
            prop: "date",
            align: "center"
          },
          {
            label: "抵扣金额",
            type: "text",
            prop: "deductPrice",
            align: "center"
          },
          {
            label: "抵扣金额",
            type: "text",
            prop: "deduct_electricity",
            align: "center"
          },
          {
            label: "卖出电量",
            type: "text",
            prop: "soldElectricity",
            align: "center"
          },
          {
            label: "卖出数量",
            type: "text",
            prop: "soldNum",
            align: "center"
          },
          {
            label: "卖出金额",
            type: "text",
            prop: "soldPrice",
            align: "center"
          },
           {
            label: "剩余电量",
            type: "text",
            prop: "surplusElectricity",
            align: "center"
          },
        ].filter(Boolean)
        }
    }
  },
  methods: {
    async getQueryData() {
      let end = this.searchs.appointmentTime ? (dateFormat(this.searchs.appointmentTime[1], "yyyy-MM-dd")) : undefined
      let begin = this.searchs.appointmentTime ? (dateFormat(this.searchs.appointmentTime[0],"yyyy-MM-dd")) : undefined
      let page =  this.pager.current
      let groups = [this.searchs.type]
      let search = objectFilter({ groups,  end, begin })
      const res = await request({
        url: "/combo/count/states",
        method: "POST",
        data: search
      });
      res.counts.map(item => {
        item.date = item.date? item.date : item.month ? item.month : item.year
        return item
      })
      this.tableData = res.counts
      // this.pager.total = res.page.totalCount
      // this.pager.current = res.page.currPage
      
    },
    pageChange(val) {
      this.pager.current = val;
      this.getQueryData();
    },
    changeSearch(key, val) {
      this[key] = val;
      this.pager.current = 1;
    },
  }
}
</script>
<style lang='scss' module>
.examine{
    display: flex;
    align-items: center;
    :global{
        div{
            color: #fff;
            padding: 10px;
            margin: 0 10px;
            border-radius: 2px;
            font-size: 14px;
            cursor: pointer;
            min-width: 60px;
            text-align: center;
        }
        .adopt{
                background: #409EFF;
        }
    }
}
.search_box {
  display: flex;
  align-items: center;
  flex-wrap: wrap;

  :global {
    .el-input {
      width: 150px;
    }
    .el-select {
      .el-input {
        width: 90px;
      }
    }
    .el-input__icon {
      line-height: 27px;
    }
    .el-range-separator{
      line-height: 26px;
    }
  }
}

.topSelect {
  background-color: white;
  border-radius: 4px;
  padding: 20px;
  margin-bottom: 20px;
}
</style>